package aps.module_Core;

//java
import java.util.ArrayList;
import java.util.List;

//aps
import aps.module_Machines.*;
import aps.module_Fusion.*;
//import aps.module_Solar.*; //disabled until I port these
//import aps.module_LaserGrid.*;
//import aps.module_SETS.*;
import aps.BuildcraftAPS;
import aps.module_Core.GuiIDs;

//buildcraft
import buildcraft.transport.Pipe;

//forge
import net.minecraft.src.ModLoader;

//vanilla minecraft
import net.minecraft.src.BlockContainer;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Material;
import net.minecraft.src.TileEntity;
import net.minecraft.src.ItemStack;
import net.minecraft.src.IBlockAccess;
import net.minecraft.src.World;
import net.minecraft.src.EntityPlayer;

public abstract class BlockAPSMeta extends BlockContainer  //removed ITexture provider, no longer in forge
{	
	int[] texes = new int[6];
	
    protected BlockAPSMeta(int i, Material m)
    {
        super(i, m);
        
        setHardness(0.5f);
    }
    
    //@Override //Used to retrieve a new instance of the TileEntity assigned to that subblock of the metablock
	public abstract TileEntity getBlockEntity(int md);
		//if (module_Core.APSMetaSubs[md] == null) return null;
		//return module_Core.APSMetaSubs[md].getBlockEntity();
    //}
    
    @Override
    public TileEntity createNewTileEntity(World world, int md)
    {
    	return getBlockEntity(md);
    }
	
    public TileEntity createNewTileEntity(World world)
    {
    	return null;
    }
    
    //@Override //Required, but not actually used.
    //public TileEntity getBlockEntity() {return null;} No longer required - Seph
    
    @Override //The texture file, simple enough.
	public String getTextureFile() {return BuildcraftAPS.imageFilesRoot + "APSBlockTexes.png";}
    
    @Override //For use by Dynamic Textures
    public int getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l)
    {
    	int meta = iblockaccess.getBlockMetadata(i, l, k);
    	TileEntity Ent = iblockaccess.getBlockTileEntity(i, j, k);
    	
    	if(Ent instanceof TileEntityAPS){ 
    		TileEntityAPS apsEnt = (TileEntityAPS)Ent;
    		texes = apsEnt.texes.clone();
    		return texes[l];
    	}
    	else return getBlockTextureFromSideAndMetadata(l, meta);
    }
    
    @Override //For display
	public int getBlockTextureFromSideAndMetadata(int i, int j)
    {
    	TileEntity Ent = getBlockEntity(j);
    	if (Ent != null) return ((TileEntityAPS) Ent).getTexture(i);
    	else return 0;
    }
    
    @Override
    public int getBlockTextureFromSide(int side)
    {
    	return texes[side];
    }

    //new onBlockRemoval method
    @Override
    public void breakBlock(World world, int i, int j, int k, int l, int m)
    {
    	TileEntity Ent = world.getBlockTileEntity(i, j, k);
    	if (Ent instanceof TileEntityAPS) ((TileEntityAPS) Ent).kill();
    	super.breakBlock(world, i, j, k, l, m);
    }
    
    //Part of the metablock code
    @Override
    public int damageDropped(int i) {return i;}
    
    @Override //Opens the selected machine's GUI if that machine has one (i.e. hasGUI is set to 'true' in its TileEntity class)
    public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer entityplayer, int l, float m, float n, float o) //updated to 1.3.2
    {
    	if (entityplayer.isSneaking()) return false; //Buildcraft trick so sneaking negates block's interaction - Seph
    	
    	TileEntityAPS Ent = (TileEntityAPS)getBlockEntity(world.getBlockMetadata(i, j, k));
    	if (Ent instanceof TileEntityAPS)
    	{
    		if (Ent.hasGUI)
    		{
    			if (!world.isRemote && Ent.GuiID > 0)
    				//ModLoader.openGUI(entityplayer, ((TileEntityAPS) Ent).getGUI(entityplayer));
    				entityplayer.openGui(BuildcraftAPS.instance, Ent.GuiID, world, i, j, k);
    			return true;
    		} else {
    			return false;
    		}
    	}
    	else
    		return false;
    }
}
